package com.sskj.xgja.login;


import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.hjq.toast.ToastUtils;
import com.sskj.xgja.MVPActivity;
import com.sskj.base.utils.CheckUtil;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.EditUtil;
import com.sskj.base.view.ToolBarLayout;
import com.sskj.xgja.R;

import butterknife.BindView;

/**
 * 忘记密码
 *
 * @author Hey
 * Create at  2019/03/21
 */
public class ForgetPasswordActivity extends MVPActivity<ForgetPasswordPresenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.forget_account)
    EditText forgetAccount;
    @BindView(R.id.verify_code)
    EditText verifyCode;
    @BindView(R.id.get_code)
    TextView getCode;
    @BindView(R.id.forget_ps)
    EditText forgetPs;
    @BindView(R.id.forget_ps2)
    EditText forgetPs2;
    @BindView(R.id.submit)
    Button submit;

    @BindView(R.id.ps_lock)
    ImageView psLock;
    @BindView(R.id.ps2_lock)
    ImageView ps2Lock;


    @Override
    public int getLayoutId() {
        return R.layout.activity_forget_password;
    }

    @Override
    public ForgetPasswordPresenter getPresenter() {
        return new ForgetPasswordPresenter();
    }

    @Override
    public void initView() {
        toolbar.setLeftButtonOnClickListener(v -> finish());
        ClickUtil.click(toolbar.getRightButton(), view -> {
            LoginActivity.start(this);
        });
        psLock.setOnClickListener(v -> {
            if (EditUtil.togglePs(forgetPs)) {
                psLock.setImageResource(R.mipmap.icon_lock);
            } else {
                psLock.setImageResource(R.mipmap.icon_unlock);
            }
        });
        ps2Lock.setOnClickListener(v -> {
            if (EditUtil.togglePs(forgetPs2)) {
                ps2Lock.setImageResource(R.mipmap.icon_lock);
            } else {
                ps2Lock.setImageResource(R.mipmap.icon_unlock);
            }
        });
    }

    @Override
    public void initData() {
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        ClickUtil.click(getCode, view -> {
            if (isEmpty(forgetAccount)) {
                ToastUtils.show("请输入资金账号");
                return;
            }
            if (!CheckUtil.isMobile(getText(forgetAccount))) {
                ToastUtils.show("账号格式不正确");
                return;
            }
            mPresenter.sendCode(getText(forgetAccount));
        });

        ClickUtil.click(submit, view -> {
            if (isEmpty(forgetAccount)) {
                ToastUtils.show("请输入资金账号");
                return;
            }
            if (!CheckUtil.isMobile(getText(forgetAccount))) {
                ToastUtils.show("账号格式不正确");
                return;
            }
            if (isEmpty(verifyCode)) {
                ToastUtils.show(R.string.input_verify_code);
                return;
            }
            if (isEmpty(forgetPs)) {
                ToastUtils.show(R.string.input_login_ps);
                return;
            }

            if (!getText(forgetPs).equals(getText(forgetPs2))) {
                ToastUtils.show(R.string.repeat_ps_error);
                return;
            }

            if (!CheckUtil.isPwd(getText(forgetPs))) {
                ToastUtils.show("密码格式错误");
                return;
            }

            mPresenter.resetPassword(getText(forgetAccount), getText(forgetPs), getText(forgetPs2), getText(verifyCode));
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, ForgetPasswordActivity.class);
        context.startActivity(intent);
    }

    public void sendCodeSuccess() {
        startTimeDown(getCode);
    }

    public void resetPasswordSuccess() {
        ToastUtils.show("修改密码成功");
        LoginActivity.start(this);
        finish();
    }
}
